package ru.dc.gui.swing.settings;

import com.jidesoft.dialog.ButtonNames;
import com.jidesoft.dialog.ButtonPanel;
import com.jidesoft.dialog.MultiplePageDialog;
import com.jidesoft.dialog.PageList;
import com.jidesoft.plaf.UIDefaultsLookup;
import com.jidesoft.swing.PartialEtchedBorder;
import ru.dc.Application;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * <pre>
 * User: Penkov Vladimir
 * Date: 02.11.2010
 * Time: 15:52:46
 * </pre>
 */
public class SettingsForm extends MultiplePageDialog {
    private InfoPage infoPage;
    private DownloadPage downloadPage;
    private ConnectionPage connectionPage;

    public SettingsForm(Frame owner) throws HeadlessException {
        super(owner);

        final PageList pageList = new PageList();
        setPageList(pageList);
        infoPage = new InfoPage();
        downloadPage = new DownloadPage();
        connectionPage = new ConnectionPage();

        getPageList().append(infoPage);
        getPageList().append(connectionPage);
        getPageList().append(downloadPage);

        setStyle(LIST_STYLE);
        setTitle("Настройки");

    }

    @Override
    protected void initComponents() {
        super.initComponents();


        setSize(640, 480);
        setLocation((int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() -getWidth())/2,
                (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() -getHeight())/3);

    }


    @Override
    public ButtonPanel createButtonPanel() {

        ButtonPanel buttonPanel = super.createButtonPanel();
        AbstractAction okAction = new AbstractAction(UIDefaultsLookup.getString("OptionPane.okButtonText")) {
            public void actionPerformed(ActionEvent e) {
                onOK();
                setDialogResult(RESULT_AFFIRMED);
                setVisible(false);
                dispose();
            }
        };
        AbstractAction cancelAction = new AbstractAction(UIDefaultsLookup.getString("OptionPane.cancelButtonText")) {
            public void actionPerformed(ActionEvent e) {
                setDialogResult(RESULT_CANCELLED);
                setVisible(false);
                dispose();
            }
        };
        ((JButton) buttonPanel.getButtonByName(ButtonNames.OK)).setAction(okAction);
        ((JButton) buttonPanel.getButtonByName(ButtonNames.CANCEL)).setAction(cancelAction);
        setDefaultCancelAction(cancelAction);
        setDefaultAction(okAction);
        return buttonPanel;
    }
    


    private static Border createSeparatorBorder() {
        return new PartialEtchedBorder(EtchedBorder.LOWERED, PartialEtchedBorder.NORTH);
    }


    private void onOK() {
        infoPage.apply();
        downloadPage.apply();
        connectionPage.apply();

        Application.getApplication().getSettings().save();
    }


}
